package boss.portal.controller;

import boss.portal.common.ResponseData;
import boss.portal.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 描述：
 */
public abstract class BaseController {

    protected Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    protected UserRepository userRepository;

    @Autowired
    protected BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 获取用户所拥有的权限列表
     * @return
     */
    public List<String> getAuthentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        List<String> list = new ArrayList<>();
        for (GrantedAuthority grantedAuthority : authorities) {
            logger.info("权限列表：{}", grantedAuthority.getAuthority());
            list.add(grantedAuthority.getAuthority());
        }
        return list;
    }

    /**
     * return success message
     *
     * @param data return data
     * @param <T>
     * @return
     */
    public <T> ResponseData success(T data) {
        ResponseData<T> response = new ResponseData<>();
        response.setCode(ResponseData.SUCCESS_CODE);
        response.setMsg(ResponseData.SUCCESS_MSG_DEFAULT);
        response.setData(data);
        response.setSuccess(true);
        return response;
    }

    /**
     * return success message
     *
     * @param msg return message
     * @param data return data
     * @param <T>
     * @return
     */
    public <T> ResponseData success(String msg, T data) {
        ResponseData<T> response = new ResponseData<>();
        response.setCode(ResponseData.SUCCESS_CODE);
        response.setMsg(msg);
        response.setData(data);
        response.setSuccess(true);
        return response;
    }

    /**
     * return success message
     *
     * @param msg return message
     * @return
     */
    public ResponseData success(String msg) {
        ResponseData response = new ResponseData();
        response.setCode(ResponseData.SUCCESS_CODE);
        response.setMsg(msg);
        response.setSuccess(true);
        return response;
    }

    /**
     * return error message
     *
     * @param msg return message
     * @return
     */
    public ResponseData error(String msg) {
        ResponseData response = new ResponseData();
        response.setCode(ResponseData.ERROR_CODE);
        response.setMsg(msg);
        response.setSuccess(false);
        return response;
    }

    public ResponseData error(int code,String msg) {
        ResponseData response = new ResponseData();
        response.setCode(code);
        response.setMsg(msg);
        response.setSuccess(false);
        return response;
    }

    /**
     * return error message
     *
     * @return
     */
    public ResponseData error() {
        ResponseData response = new ResponseData();
        response.setCode(ResponseData.ERROR_CODE);
        response.setMsg(ResponseData.ERROR_MSG_DEFAULT);
        response.setSuccess(false);
        return response;
    }
}
